/*
*	Copyright (C) 2014  Julio Montes, Mario Carrillo
*	
*	Julio Montes e-mail: imc.coder@gmail.com 
*
*	Mario Carrillo e-mail: mario_90ago@live.com.mx
*
*	BSD 3-Clause License
*/

#include "alliance/include/logmgr.h"
#include "alliance/include/path.h"
#include "alliance/include/project.h"

#include "include/iaManager.h"

#include <wx/imaglist.h>

namespace alliance
{
    template<> LogManager *sdk::Mgr<LogManager>::instance = 0;

    LogManager::LogManager()
        : m_notebook( NULL )
    {

    }

    LogManager::~LogManager()
    {

    }

    bool LogManager::OnClose()
    {
        //!TODO: implement
        return true;
    }

    bool LogManager::Load()
    {
        wxAuiNotebook *aui = iaManager::Get()->GetLogNotebook();
        if( !aui )
        {
            //TODO: show error
            return false;
        }

        m_notebook = new wxAuiNotebook( aui, wxID_ANY );
        aui->AddPage( m_notebook, wxT( "alliance log" ) );

        return true;
    }

    void LogManager::ClearLogProject( Project *project )
    {
        NotebookLogProject *logProject = GetLogProject( project );
        logProject->DeleteAllPages();
    }

    void LogManager::ShowLog( Project *project, const Tool &tool, ImageStatus status, const wxString &stdinStr, const wxString &stderrStr )
    {
        NotebookLogProject *logProject = GetLogProject( project );
        logProject->AddLog( tool, status, stdinStr, stderrStr );
    }

    NotebookLogProject *LogManager::GetLogProject( Project *project ) const
    {
        const wxString &pathProject = project->GetFilename();
        for( size_t i=0; i<m_notebook->GetPageCount(); ++i )
        {
            NotebookLogProject *logProject = dynamic_cast<NotebookLogProject *>( m_notebook->GetPage( i ) );
            if( pathProject == logProject->GetId() )
            {
                m_notebook->SetSelection( i );
                return logProject;
            }
        }

        NotebookLogProject *newLogProject = new NotebookLogProject( m_notebook, pathProject );
        m_notebook->AddPage( newLogProject, project->GetName(), true );
        return newLogProject;
    }












    NotebookLogProject::NotebookLogProject( wxWindow *parent, const wxString &id )
        : wxNotebook( parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxNB_LEFT )
        , m_id( id )
    {
        wxImageList *imgList = new wxImageList;
        imgList->Add( wxBitmap( Path::s_dirImages + wxT( "success.png" ), wxBITMAP_TYPE_PNG ) );
        imgList->Add( wxBitmap( Path::s_dirImages + wxT( "warning.png" ), wxBITMAP_TYPE_PNG ) );
        imgList->Add( wxBitmap( Path::s_dirImages + wxT( "error.png" ), wxBITMAP_TYPE_PNG ) );
        AssignImageList( imgList );
    }

    NotebookLogProject::~NotebookLogProject()
    {

    }

    const wxString &NotebookLogProject::GetId() const
    {
        return m_id;
    }

    void NotebookLogProject::AddLog( const Tool &tool, LogManager::ImageStatus status, const wxString &stdinStr, const wxString &stderrStr )
    {
        LogTool *logTool = new LogTool( this );
        logTool->SetLog( tool, stdinStr, stderrStr );
        AddPage( logTool, tool.GetName(), true, status );
    }














    LogTool::LogTool( wxWindow *parent )
    #ifdef USE_RICHTEXTCTRL
        : wxRichTextCtrl( parent )
    #else
        : wxTextCtrl( parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE )
    #endif // USE_RICHTEXTCTRL
    {
        SetFont( wxFont( 10, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, _T( "Monospace" ) ) );
        SetEditable( false );
    }

    LogTool::~LogTool()
    {

    }

    void LogTool::SetLog( const Tool &tool, const wxString &stdinStr, const wxString &stderrStr )
    {
    #ifdef USE_RICHTEXTCTRL
        BeginBold();
        WriteText( tool.GetCommandForFile() + wxT( "\n" ) );
        EndBold();
        Newline();
        BeginTextColour( wxColour( _T( "#FF0000" ) ) );
        WriteText( stderrStr );
        EndTextColour();
        Newline();
        WriteText( stdinStr );
    #else
        SetValue( stdinStr + _T( "\n\n" ) + stderrStr );
    #endif // USE_RICHTEXTCTRL
    }
}


